home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 173_01 / word.lxi < prev    next >
Text File  |  1980-01-01  |  2KB  |  75 lines

  1.  
  2. /*
  3.  * Count words -- interactively
  4.  */
  5. white   = [\n\t ];              /* End of a word        */
  6. eol     = [\0];                 /* End of input line    */
  7. any     = [!-~];                /* All printing char's  */
  8. illegal = [\0-\377];            /* Skip over junk       */
  9. %{
  10. char            line[133];
  11. char            *linep          = line;
  12. int             is_eof          = 0;
  13. int             wordct          = 0;
  14. #define T_EOL   1
  15. main()
  16. {
  17.         register int    i;
  18.         while ((i = yylex()) != 0) {
  19.                 /*
  20.                  * If the "end-of-line" token is  returned
  21.                  * AND  we're really at the end of a line,
  22.                  * read the next line.  Note that T_EOL is
  23.                  * returned  twice when the program starts
  24.                  * because of the nature of the look-ahead
  25.                  * algorithms.
  26.                  */
  27.                 if (i == T_EOL && !is_eof && *linep == 0) {
  28.                         printf("* ");
  29.                         getline();
  30.                 }
  31.         }
  32.         printf("%d words\n", wordct);
  33. }
  34. %}
  35. %%
  36.  
  37. any(any)*       {
  38.                         /*
  39.                          * Write each word on a seperate line
  40.                          */
  41.                         lexecho(stdout);
  42.                         printf("\n");
  43.                         wordct++;
  44.                         return(LEXSKIP);
  45.                 }
  46. eol             {
  47.                         return(T_EOL);
  48.                 }
  49. white(white)*   {
  50.                         return(LEXSKIP);
  51.                 }
  52. %%
  53.  
  54. getline()
  55. /*
  56.  * Read a line for lexgetc()
  57.  */
  58. {
  59.     extern char *fgets();
  60.  
  61.         is_eof = (fgets(line, sizeof line, stdin) == NULL);
  62.         linep = line;
  63. }
  64.  
  65.  
  66. lexgetc()
  67. /*
  68.  * Homemade lexgetc -- return zero while at the end of an
  69.  * input line or EOF at end of file.  If more on this line,
  70.  * return it.
  71.  */
  72. {
  73.         return((is_eof) ? EOF : (*linep == 0) ? 0 : *linep++);
  74. }
  75.